package com.cn.demo4;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Example3 {
    public void publicMethod(String message) {
        System.out.println("Message: " + message);
    }



    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        //Method getMethod(String name, Class<?>... parameterTypes)
        //获取指定名称和参数类型的公共方法。
        Class<Example3> clazz = Example3.class;
        //获取方法对象
        Method method = clazz.getMethod("publicMethod", String.class);
        //调用方法
        Example3 example3 = new Example3();
        method.invoke(example3,"hello,reflection");
    }
}
